package com.cn.springcloudwechat.controller.personalcenter;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.EmptyUtil;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.utils.base.Const;
import com.cn.springcloudwechat.controller.vaccine.log.LogInfo;
import com.cn.springcloudwechat.controller.vaccine.log.OperateType;
import com.cn.springcloudwechat.mapper.mapper.YmUserPointMapper;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.YmUserPoint;
import com.cn.springcloudwechat.redis.RedisUtil;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserPointService;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/web/user/")
public class UserController{

    @Autowired
    YmUserService ymUserService;

    @Autowired
    YmUserPointService ymUserPointService;

    /**
     * 修改用户资料（前后台通用）
     * @param ajaxRes
     * @param ymUser
     * @return
     */
    @RequestMapping("updateUser")
    @LogInfo(operateType = OperateType.UPDATE, content = "修改用户资料（前后台通用）")
    public AjaxRes updateUser(AjaxRes ajaxRes, YmUser ymUser){
        try{
            if(EmptyUtil.objFieldIsEmpty(ymUser,"userId")){
                ymUser.setUserId(ThreadLocalUtil.getUser().getUserId());
            }
            if(ymUser.getHeadimgurl() != null){
                ymUserPointService.insertUserPoint(ymUser.getUserId(),"上传头像获得",2,"+20");
            }
            Integer result = ymUserService.updateUser(ymUser);
            ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("getUser")
    @LogInfo(operateType = OperateType.QUERY, content = "获取一个用户")
    public AjaxRes getUser(AjaxRes ajaxRes){


        try{
            YmUser ymUser = ThreadLocalUtil.getUser();
           // YmUser user = ymUserService.getUserByPhone("18090031006");
            YmUser user = ymUserService.getUserByPhone(ymUser.getUserPhone());

            ajaxRes.setSucceed(user,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }


    /**
     * 获取一个测试用户
     * @param ajaxRes
     * @return
     */
    @RequestMapping("getTestUser")
    @LogInfo(operateType = OperateType.QUERY, content = "获取一个用户")
    public AjaxRes getTestUser(AjaxRes ajaxRes){
        try{
            YmUser ymUser = ThreadLocalUtil.getUser();
            //YmUser ymUser = ymUserService.getUserByPhone("15928163873");
//            YmUser ymUser = ymUserService.getUserByPhone("17602860364");
            ajaxRes.setSucceed(ymUser,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     *查询用户号码
     * @return
     */
    @RequestMapping("getUserPhone")
    @LogInfo(operateType = OperateType.QUERY, content = "查询用户号码")
    public AjaxRes getUserPhone(AjaxRes ajaxRes){
        try{
            YmUser ymUser = ThreadLocalUtil.getUser();
            String userPhone = ymUser.getUserPhone();
            ajaxRes.setSucceed(userPhone,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 获取用户豆豆总数
     * @param ajaxRes
     * @return
     */
    @RequestMapping("getNewestPoint")
    @LogInfo(operateType = OperateType.QUERY, content = "获取用户豆豆总数")
    public AjaxRes getNewestPoint(AjaxRes ajaxRes){
        try{
            Long userId = ThreadLocalUtil.getUser().getUserId();
            YmUserPoint ymUserPoint = ymUserPointService.getNewestPoint(userId);
            Long total = ymUserPoint.getTotalPoint();
            ajaxRes.setSucceed(total,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("getPoint")
    @LogInfo(operateType = OperateType.QUERY, content = "豆豆明细")
    public AjaxRes getPoint(AjaxRes ajaxRes,Page<YmUserPoint> page){
        try{
            Long userId=ThreadLocalUtil.getUser().getUserId();
            Page<YmUserPoint> list=ymUserService.getPoint(userId,page);
            ajaxRes.setSucceed(list,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }





}
